package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/louloulin/dataflare/pkg/workflow"
)

func main() {
	// Parse workflow definition
	workflowPath := "examples/workflow/simple-workflow.yaml"
	if len(os.Args) > 1 {
		workflowPath = os.Args[1]
	}

	fmt.Printf("Testing workflow: %s\n", workflowPath)

	definition, err := workflow.ParseFromFile(workflowPath)
	if err != nil {
		log.Fatalf("Failed to parse workflow: %v", err)
	}

	fmt.Printf("Parsed workflow: %s (%s)\n", definition.Name, definition.Description)

	// Create engine
	engine, err := workflow.NewEngine()
	if err != nil {
		log.Fatalf("Failed to create engine: %v", err)
	}
	defer engine.Shutdown()

	// Deploy workflow
	ctx := context.Background()
	fmt.Println("Deploying workflow...")
	workflowID, err := engine.DeployWorkflow(ctx, definition)
	if err != nil {
		log.Fatalf("Failed to deploy workflow: %v", err)
	}
	fmt.Printf("Workflow deployed with ID: %s\n", workflowID)

	// Start workflow
	fmt.Println("Starting workflow...")
	runID, err := engine.StartWorkflow(ctx, workflowID, nil)
	if err != nil {
		log.Fatalf("Failed to start workflow: %v", err)
	}
	fmt.Printf("Workflow started with run ID: %s\n", runID)

	// Wait for workflow to complete
	fmt.Println("Waiting for workflow to complete...")
	for {
		status, err := engine.GetWorkflowStatus(ctx, runID)
		if err != nil {
			log.Fatalf("Failed to get workflow status: %v", err)
		}

		fmt.Printf("Workflow status: %s (Progress: %d%%)\n", status.Status, status.Progress)

		if status.Status == "completed" || status.Status == "failed" {
			break
		}

		time.Sleep(1 * time.Second)
	}

	fmt.Println("Workflow execution completed!")
}
